/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Attributes;

import Attributes.AttributeCleaner;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

/**
 *
 * @author Alexander
 */
public class getBeatportInfo {

    private static ArrayList<String> valSet;
    private static String Track_URL;
    private static String PurchaseLink;
    private static String Path = "";
    private static String album;
    private static String genre;
    private static boolean resetNoNum;

    public static ArrayList<String> getInfo(String title, String artist) {
        System.out.println("getBeatportInfo.getInfo(" + title + ", " + artist + ")");

        album = null;
        genre = null;
        PurchaseLink = "";
        resetNoNum = false;
        title = AttributeCleaner.cleanAttribute("Title", title).replaceAll(" ", "%20");
        artist = AttributeCleaner.cleanAttribute("Artist", artist).replaceAll(" ", "%20");
        startConnection(title, artist);

        return valSet;
    }

    public static void startConnection(String title, String artist) {
        System.out.println("getBeatportInfo.startConnection(" + title + ", " + artist + ")");
        valSet = new ArrayList();
        String site = "https://www.beatport.com/search?query=" + title + "%20" + artist + "&facets[0]=fieldType%3Atrack";
        System.out.println(site);
        PurchaseLink = site;
        try {
            Document doc = Jsoup.connect(site).get();
            Elements link = doc.select("span[class = play-queue play-queue-med-small]");
            String musicLink = link.attr("data-json");
            //JSONObject outerObject = null;
            JSONObject objectInArray = null;
            objectInArray = new JSONObject(musicLink);
            valSet.add(getTrackURL(objectInArray));
            valSet.add(getPurchaseLink(site));
            valSet.add(getAlbum(objectInArray));
            valSet.add(getGenre(objectInArray));
            //valSet.add(getTitle(objectInArray));
            //valSet.add(getArtist(objectInArray));
            valSet.add(getBPM(objectInArray));
            valSet.add(getKey(objectInArray));
        } catch (IOException | JSONException ex) {
            System.err.println("Error Connecting to Beatport");
            valSet.add("");
            valSet.add("");
            valSet.add("");
            valSet.add("");
            //valSet.add(title);
            //valSet.add(artist);
            valSet.add("");
            valSet.add("");
        }
    }

    public static String getTrackURL(JSONObject objectInArray) {
        String TrackURL;
        try {
            TrackURL = objectInArray.getString("sampleUrl");
        } catch (JSONException ex) {
            TrackURL = "";
        }
        System.out.println("getTrackURL() - Returns - " + TrackURL);
        return TrackURL;
    }

    public static String getPurchaseLink(String site) {
        String link = site;
        System.out.println("getPurchaseLink() - Returns - " + link);
        return link;
    }

    public static String getAlbum(JSONObject objectInArray) {
        String album;
        try {
            album = AttributeCleaner.clean(objectInArray.getString("mixName"));
        } catch (JSONException ex) {
            album = "";
        }
        System.out.println("getAlbum() - Returns - " + album);
        return album;
    }

    public static String getGenre(JSONObject objectInArray) {
        String genre;
        try {
            JSONArray genreArray = objectInArray.getJSONArray("genres");
            objectInArray = genreArray.getJSONObject(0);
            genre = objectInArray.getString("name");
        } catch (JSONException ex) {
            genre = "";
        }
        System.out.println("getGenre() - Returns - " + genre);
        return genre;
    }

    public static String getBPM(JSONObject objectInArray) {
        String bpm = objectInArray.optString("bpm");
        if (bpm.equals("")) {
            bpm = "000";
        }
        System.out.println("getBPM() - Returns - " + bpm);
        return bpm;
    }

    public static String getKey(JSONObject objectInArray) {
        String key;
        try {
            JSONObject keyObject = objectInArray.getJSONObject("key");
            key = keyObject.optString("shortName").replace("min", "").replace("maj", "").replace("&#9839", "");
        } catch (JSONException ex) {
            key = "";
        }
                System.out.println("getKey() - Returns - " + key);
        return key;
    }
    /*
     public static String getTitle(JSONObject objectInArray) {
     String title;
     try {
     title = AttributeCleaner.cleanAttribute("Title", objectInArray.getString("name"));
     } catch (JSONException ex) {
     title = "";
     }
     return title;
     }

     public static String getArtist(JSONObject objectInArray) {
     String artist;
     try {
     JSONArray artistArray = objectInArray.getJSONArray("artists");
     objectInArray = artistArray.getJSONObject(0);
     artist = AttributeCleaner.cleanAttribute("Artist", objectInArray.getString("name"));
     } catch (JSONException ex) {
     artist = "";
     }
     return artist;
     }
     */
}
